//
//  NSString+Time.m
//  Public
//
//  Created by wangcl on 2018/5/15.
//  Copyright © 2018年 wangcl. All rights reserved.
//

#import "NSString+Time.h"

@implementation NSString (Time)
/** 将不带标点符号的时间字符串，转化为带类型2017.08.06格式的时间字符串 */
+ (NSString *)cutOffWithTimeStr:(NSString *)timeStr formatter:(TimeFormatter)timeFormatter
{
    if(timeStr.length == 0)  return timeStr;
    
    NSString *year   = [timeStr substringWithRange:NSMakeRange(0, 4)];
    NSString *month  = [timeStr substringWithRange:NSMakeRange(4, 2)];
    NSString *day    = [timeStr substringWithRange:NSMakeRange(6, 2)];
    NSString *hours;
    NSString *minute;
    NSString *second;
    NSString *time;
    if (timeFormatter == TimeFormatter_YMDHMS_Point || timeFormatter == TimeFormatter_YMDHMS || timeFormatter == TimeFormatter_YMDHMS_Line) {
        hours  = [timeStr substringWithRange:NSMakeRange(8, 2)];
        minute = [timeStr substringWithRange:NSMakeRange(10, 2)];
        second = [timeStr substringWithRange:NSMakeRange(12, 2)];
    }
    
    switch (timeFormatter) {
        case TimeFormatter_YMD:
            time = [NSString stringWithFormat:@"%@年%@月%@日",year,month,day];
            break;
        case TimeFormatter_YMDHMS:
            time = [NSString stringWithFormat:@"%@年%@月%@日 ",year,month,day];
            break;
            
        case TimeFormatter_YMD_Point:
            time = [NSString stringWithFormat:@"%@.%@.%@",year,month,day];
            break;
            
        case TimeFormatter_YMDHMS_Point:
            time = [NSString stringWithFormat:@"%@.%@.%@ %@:%@:%@ ",year,month,day,hours,minute,second];
            break;
            
        case TimeFormatter_YMD_Line:
            time = [NSString stringWithFormat:@"%@-%@-%@",year,month,day];
            break;
        case TimeFormatter_YMDHMS_Line:
            time = [NSString stringWithFormat:@"%@-%@-%@ %@:%@:%@ ",year,month,day,hours,minute,second];
            break;
            
        default:
            break;
    }
    return time;
}

#pragma mark 将不带标点符号的时间字符串，转化为带类型2017.08.06格式的时间字符串

+ (NSString *)cutOffWithTimeStr:(NSString *)timeStr
{
    if (timeStr.length == 0) {
        return timeStr;
    }
    NSString *year   = [timeStr substringWithRange:NSMakeRange(0, 4)];
    NSString *month  = [timeStr substringWithRange:NSMakeRange(4, 2)];
    NSString *day    = [timeStr substringWithRange:NSMakeRange(6, 2)];
    NSString *time   = [NSString stringWithFormat:@"%@.%@.%@",year,month,day];
    return time;
}

#pragma mark 返回当前时间前后N天的时间

+ (NSString *)getTimeStrWithDay:(NSInteger)num WithFormatter:(NSString *)format
{
    NSDate*nowDate = [NSDate date];
    NSDate* theDate;
    NSTimeInterval  oneDay = 24*60*60*1;  //1天的长度
    //之后的天数
    theDate = [nowDate initWithTimeIntervalSinceNow: +oneDay*num];
    //实例化一个NSDateFormatter对象
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    //设定时间格式,这里可以设置成自己需要的格式
    [dateFormatter setDateFormat:format];
    //用[NSDate date]可以获取系统当前时间
    NSString * currentDateStr = [dateFormatter stringFromDate:theDate];
    
    return currentDateStr;
}

#pragma mark - 获取当前时间的 时间戳

+(NSInteger)getNowTimestamp
{
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateStyle:NSDateFormatterMediumStyle];
    [formatter setTimeStyle:NSDateFormatterShortStyle];
    [formatter setDateFormat:@"YYYY-MM-dd HH:mm:ss"]; // ----------设置你想要的格式,hh与HH的区别:分别表示12小时制,24小时制
    //设置时区,这个对于时间的处理有时很重要
    NSTimeZone* timeZone = [NSTimeZone timeZoneWithName:@"Asia/Beijing"];
    [formatter setTimeZone:timeZone];
    NSDate *datenow = [NSDate date];//现在时间
    //时间转时间戳的方法:
    NSInteger timeSp = [[NSNumber numberWithDouble:[datenow timeIntervalSince1970]] integerValue]*1000;
    return timeSp;
}


#pragma mark - 将某个时间转化成 时间戳

+(NSInteger)timeSwitchTimestamp:(NSString *)formatTime andFormatter:(NSString *)format{
    
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateStyle:NSDateFormatterMediumStyle];
    [formatter setTimeStyle:NSDateFormatterShortStyle];
    //(@"YYYY-MM-dd hh:mm:ss") ----------设置你想要的格式,hh与HH的区别:分别表示12小时制,24小时制
    [formatter setDateFormat:format];
    NSTimeZone* timeZone = [NSTimeZone timeZoneWithName:@"Asia/Beijing"];
    [formatter setTimeZone:timeZone];
    NSDate* date = [formatter dateFromString:formatTime]; //------------将字符串按formatter转成nsdate
    //时间转时间戳的方法:
    NSInteger timeSp = [[NSNumber numberWithDouble:[date timeIntervalSince1970]] integerValue];
    return timeSp;
    
}

+(NSString *)timestampSwitchTime:(NSInteger)timestamp andFormatter:(NSString *)format
{
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateStyle:NSDateFormatterMediumStyle];
    [formatter setTimeStyle:NSDateFormatterShortStyle];
    [formatter setDateFormat:format]; // （@"YYYY-MM-dd hh:mm:ss"设置你想要的格式,hh与HH的区别:分别表示12小时制,24小时制
    NSTimeZone *timeZone = [NSTimeZone timeZoneWithName:@"Asia/Beijing"];
    [formatter setTimeZone:timeZone];
    NSDate *confromTimesp = [NSDate dateWithTimeIntervalSince1970:timestamp/1000];
    NSString *confromTimespStr = [formatter stringFromDate:confromTimesp];
    return confromTimespStr;
}


#pragma mark - 将某个时间戳转化成 时间

+(NSString *)changeTimeFormat:(NSString *)timeStr andFormatter:(NSString *)format
{
    NSDateFormatter *inputFormatter= [[NSDateFormatter alloc] init];
    [inputFormatter setTimeZone:[NSTimeZone timeZoneWithName:@"Asia/Shanghai"]];
    [inputFormatter setDateFormat:@"yyyyMMdd"];
    NSDate *inputDate = [inputFormatter dateFromString:timeStr];
    
    NSDateFormatter *outputFormatter= [[NSDateFormatter alloc] init];
    [outputFormatter setLocale:[NSLocale currentLocale]];
    [outputFormatter setDateFormat:format];//@"yyyy年MM月dd日 HH时mm分ss秒"
    NSString *str= [outputFormatter stringFromDate:inputDate];
    if (str.length == 0) {
        str = timeStr;
    }
    return str;
}

/** 转换时间格式  如20171011 转为 2017-10-11  */
+(NSString *)changeTimeFormat:(NSString *)timeStr inputTimeFormat:(NSString *)input outputTimeFormat:(NSString *)output
{
    //转换时间格式
    NSDateFormatter *df = [[NSDateFormatter alloc] init];//格式化
    [df setDateFormat:input];
    NSDate * date = [df dateFromString:timeStr];
    
    //转换时间格式
    NSDateFormatter *df2 = [[NSDateFormatter alloc] init];//格式化
    [df2 setDateFormat:output];
    [df2 setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"zh_CN"]];
    NSString *strVlae = [df2 stringFromDate:date];
    return strVlae;
}

/** 处理小数点精度  直接转会四舍五入所以使用NSDecimalNmberHandler处理 */
+(NSString *)dealStrPoint:(CGFloat)price
{
    CGFloat money = price + 0.0001;
    NSDecimalNumberHandler* roundingBehavior = [NSDecimalNumberHandler decimalNumberHandlerWithRoundingMode:NSRoundDown scale:2 raiseOnExactness:NO raiseOnOverflow:NO raiseOnUnderflow:NO raiseOnDivideByZero:NO];
    NSDecimalNumber *ouncesDecimal = [[NSDecimalNumber alloc] initWithDouble:money];
    NSDecimalNumber *roundedOunces = [ouncesDecimal decimalNumberByRoundingAccordingToBehavior:roundingBehavior];
    NSString *moneyStr = [NSString stringWithFormat:@"%@",roundedOunces];
    return [NSString stringWithFormat:@"%.02lf",[moneyStr doubleValue]];;
}

/** 获取当前日期和指定日期之间的天数 */
+(NSInteger)numberOfDaysWithTime:(NSString *)time
{
    NSString *dateStr = [NSString stringWithFormat:@"%@ 23:00:00",time];
    NSInteger timestamp = [NSString timeSwitchTimestamp:dateStr andFormatter:@"YYYYMMdd HH:mm:ss"];
    NSDate *maxDate = [NSDate dateWithTimeIntervalSince1970:timestamp];
    NSDate *nowDate = [NSDate date];
    NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
    NSDateComponents *comp = [calendar components:NSCalendarUnitDay fromDate:nowDate toDate:maxDate options:NSCalendarWrapComponents];
    
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"yyyyMMdd"];
    NSTimeZone* timeZone = [NSTimeZone timeZoneWithName:@"Asia/Beijing"];
    [formatter setTimeZone:timeZone];
    NSString *nowTime = [formatter stringFromDate:nowDate];
    if ([time isEqualToString:nowTime]) {
        return 1;
    }
    if (comp.day == 0) {
        return 0;
    }else{
        return comp.day+1;
    }
}
+(NSString *)timeToDate:(NSDate *)date format:(NSString *)format
{
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
    NSTimeZone *timeZone = [NSTimeZone timeZoneWithName:@"Asia/Beijing"];
    [dateFormatter setTimeZone:timeZone];
    [dateFormatter setDateFormat:format];
    NSString *dateStr = [dateFormatter stringFromDate:date];
    return dateStr;
}
@end
